package mumethod;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.text.TableView.TableRow;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import muobject.Question;
import muobject.School;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExportFunction {

	public void ExportSchoolVerticalExcel(List<String> columns, List<String> values,
			String path) {
		System.setProperty("file.encoding", "Big5");
		try {
			OutputStream os = new FileOutputStream(path, false);
			WorkbookSettings wbsettings = new WorkbookSettings();
			wbsettings.setEncoding("Big5"); 
			WritableWorkbook workbook = Workbook.createWorkbook(os, wbsettings);
			WritableSheet sheet = workbook.createSheet("First Sheet", 0);
			WritableFont wfont =new jxl.write.WritableFont(WritableFont.ARIAL,14,
                     WritableFont.BOLD,false,
                     UnderlineStyle.NO_UNDERLINE);
			sheet.setColumnView(1, 130);
			WritableCellFormat wcFormat =new jxl.write.WritableCellFormat(wfont);
			int c = 0 ,v = 0;
			for (String column : columns) {
				Label label = new Label(0, c, column, wcFormat);
				sheet.addCell(label);
				c++;
			}
			for (String value : values) {
				Label label = new Label(1, v, value, wcFormat);
				sheet.addCell(label);
				v++;
			}
			sheet.setColumnView(0, 30);
			sheet.setColumnView(1, 100);
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	public List<String> ListColumns(String... columns) {
		List<String> results = new ArrayList<String>();
		for (String column : columns) {
			results.add(column);
		}
		return results;
	}
	
	public Boolean ExportStudentTemplateExcel(String templatePath, String destinationPath){
		try {
			String SoursePath = templatePath + "Student.xls";
			byte[] buffer = new byte[1024]; 
			FileInputStream fileInputStream = new FileInputStream(new File(SoursePath));
			FileOutputStream fileOutputStream =  new FileOutputStream(new File(destinationPath)); 
			 int length = -1;
			 while((length = fileInputStream.read(buffer)) != -1) { 
				 fileOutputStream.write(buffer, 0, length);
				 }  
			 fileInputStream.close(); 
			 fileOutputStream.close(); 
			 return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		} 
	}
	
	public Boolean ExportPhysicalTemplateExcel(Table dataTable, String destinationPath){
		
		WorkbookSettings wbsettings;
		WritableWorkbook workbook;
		WritableSheet sheet;
		OutputStream os;
	    try {
	    	os = new FileOutputStream(destinationPath, false);
			wbsettings = new WorkbookSettings();
			wbsettings.setEncoding("Big5"); 
			workbook = Workbook.createWorkbook(os, wbsettings);
			sheet = workbook.createSheet("Physical", 0);
			WritableFont wfont =new jxl.write.WritableFont(WritableFont.ARIAL,14,
	                 WritableFont.BOLD,false,
	                 UnderlineStyle.NO_UNDERLINE);
			sheet.setColumnView(1, 130);
			WritableCellFormat wcFormat =new jxl.write.WritableCellFormat(wfont);
			int c = 0 ,r = 0;
			
			for (TableColumn column : dataTable.getColumns()) {
				if(c != 0){
					Label label = new Label(c-1, 0, column.getText(), wcFormat);
					sheet.addCell(label);
				}
				c++;
			}
			c = 0;
			for (TableColumn column : dataTable.getColumns()) {
				r = 1;
				if(c < 8 & c != 0){
					if(!column.getText().equals("")){
						for (TableItem item : dataTable.getItems()) {
							if(c==1){
								DateControl dateControl = new LibFunction();
								Date cDate = dateControl.getStringDate(item.getText(c));
								jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-m-d"); 
								jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(wfont, df); 
								jxl.write.DateTime labelDTF   = new jxl.write.DateTime(c-1, r, cDate, wcfDF);
								sheet.addCell(labelDTF);
							}else{
								Label label = new Label(c-1, r, item.getText(c), wcFormat);
								sheet.addCell(label);
							}
							r ++;
						}
					}
				}
				sheet.setColumnView(c, 30);
				c++;
			}
			workbook.write();
			workbook.close();
			os.close();
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
		return true;
	}
	
	public boolean ExportTableExcel(Table dataTable, String path, String name){
		
		WorkbookSettings wbsettings;
		WritableWorkbook workbook;
		WritableSheet sheet;
		OutputStream os;
	    try {
	    	os = new FileOutputStream(path, false);
			wbsettings = new WorkbookSettings();
			wbsettings.setEncoding("Big5"); 
			workbook = Workbook.createWorkbook(os, wbsettings);
			sheet = workbook.createSheet(name, 0);
			WritableFont wfont =new jxl.write.WritableFont(WritableFont.ARIAL,14,
	                 WritableFont.BOLD,false,
	                 UnderlineStyle.NO_UNDERLINE);
			sheet.setColumnView(1, 130);
			WritableCellFormat wcFormat =new jxl.write.WritableCellFormat(wfont);
			int c = 0 ,r = 0;
			
			for (TableColumn column : dataTable.getColumns()) {
				Label label = new Label(c, 0, column.getText(), wcFormat);
				sheet.addCell(label);
				c++;
			}
			c = 0;
			for (TableColumn column : dataTable.getColumns()) {
				r = 1;
				if(!column.getText().equals("")){
					for (TableItem item : dataTable.getItems()) {
						Label label = new Label(c, r, item.getText(c), wcFormat);
						sheet.addCell(label);
						r ++;
					}
					sheet.setColumnView(c, 30);
				}
				c++;
			}
			workbook.write();
			workbook.close();
			os.close();
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
		return true;
	}
	
	public boolean ExportQuestionnaireTemplateExcel(Table dataTable, List<Question> questions, String destinationPath){
		WorkbookSettings wbsettings;
		WritableWorkbook workbook;
		WritableSheet sheet;
		OutputStream os;
		Label label;
	    try {
	    	os = new FileOutputStream(destinationPath, false);
			wbsettings = new WorkbookSettings();
			wbsettings.setEncoding("Big5"); 
			workbook = Workbook.createWorkbook(os, wbsettings);
			sheet = workbook.createSheet("Physical", 0);
			WritableFont wfont =new jxl.write.WritableFont(WritableFont.ARIAL,14,
	                 WritableFont.BOLD,false,
	                 UnderlineStyle.NO_UNDERLINE);
			sheet.setColumnView(1, 130);
			WritableCellFormat wcFormat =new jxl.write.WritableCellFormat(wfont);
			int c = 0 ,r = 0;
			
			for (TableColumn column : dataTable.getColumns()) {
				if(c != 0){
					label = new Label(c-1, 0, column.getText(), wcFormat);
					sheet.addCell(label);
					sheet.setColumnView(c-1, 20);
				}
				c++;
			}
			
			label = new Label(c-1, 0, "測試日期", wcFormat);
			sheet.addCell(label);
			sheet.setColumnView(c-1, 20);
			c++;
			
			for (Question q : questions){
				label = new Label(c-1, 0, q.getQno(), wcFormat);
				sheet.addCell(label);
				c++;
			}
			
			
			c = 0;
			for (TableColumn column : dataTable.getColumns()) {
				r = 1;
				if(c != 0){
					if(!column.getText().equals("")){
						for (TableItem item : dataTable.getItems()) {
							label = new Label(c-1, r, item.getText(c), wcFormat);
							sheet.addCell(label);
							if(c==6){
								DateControl dateControl = new LibFunction();
								Date cDate = dateControl.getStringDate("1900-1-1");
								jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-m-d"); 
								jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(wfont, df); 
								jxl.write.DateTime labelDTF   = new jxl.write.DateTime(6, r, cDate, wcfDF);
								sheet.addCell(labelDTF);
							}
							r ++;
						}
					}
				}
				c++;
			}
			workbook.write();
			workbook.close();
			os.close();
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
		return true;
	}
}
